//
//  LKCycleView.m
//  轮播图封装(CollectionView版本)
//
//  Created by 冷坤 on 16/5/26.
//  Copyright © 2016年 冷坤. All rights reserved.
//

#import "LKCycleView.h"
#import "LKCycleViewCell.h"
#import "SlideModel.h"
#import "UIImageView+WebCache.h"
#define SECTION_COUNT 1000
@interface LKCycleView ()<UICollectionViewDataSource, UICollectionViewDelegate,UIScrollViewDelegate>
/**轮播图控件*/
@property (nonatomic, strong)UICollectionView *collectionView;
/**数据源数组*/
@property (nonatomic, strong)NSArray *dataSourceArr;
/**定时器时间间隔*/
@property (nonatomic, assign)CGFloat timeInterval;
/**定时器对象*/
@property (nonatomic, strong)NSTimer *cyceTimer;
/**当前item下标*/
@property (nonatomic, assign)NSInteger itemIndex;
/**当前section下标*/
@property (nonatomic, assign)NSInteger currentSection;
@end

@implementation LKCycleView

- (instancetype)initWithFrame:(CGRect)frame DataSource:(NSArray *)dataSoureArray TimeInterval:(CGFloat)time{
    self = [super initWithFrame:frame];
    if (self) {
        // 将外界传递进来的数据源赋值给dataSourceArr
        
        self.dataSourceArr = dataSoureArray;
        
        // 初始化默认瀑布流样式
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = frame.size;
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(00, 0, 0, 0);
        layout.scrollDirection = 1;
        // 初始化collectionView
        self.collectionView = [[UICollectionView alloc]initWithFrame:frame collectionViewLayout:layout];
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        self.collectionView.pagingEnabled = YES;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        [self addSubview:self.collectionView];
        // 注册cell
        [self.collectionView registerClass:[LKCycleViewCell class] forCellWithReuseIdentifier:@"LK_Cell"];
        // 设置初始偏移量
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:SECTION_COUNT / 2] atScrollPosition:(UICollectionViewScrollPositionLeft) animated:NO];
        // 判断时间是否合理
        if (time > 0) {
            self.timeInterval = time;
            [self addTimer];
        }
    }
    return self;
}
#pragma mark - 点击item触发的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"-------%ld",indexPath.item);
    // 命令代理人去执行代理方法 :
    if (self.delegate != nil) {
        [self.delegate didClickedSelectItemAtIndexPath:indexPath];
    }
}
#pragma mark - 添加定时器:
- (void)addTimer{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:self.timeInterval target:self selector:@selector(changePage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    self.cyceTimer = timer;
}
#pragma mark - 移除定时器
- (void)removeTimer{
    [self.cyceTimer invalidate];
    self.cyceTimer = nil;
}
#pragma mark - 定时器调用的方法
- (void)changePage{
    if (_itemIndex == self.dataSourceArr.count) {
        _itemIndex = 0;
        _currentSection++;
    }
   // 偏移到最新位置 (下一张)
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_itemIndex inSection:_currentSection] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    _itemIndex++;
}
#pragma mark - item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}
#pragma mark - section的个数:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return SECTION_COUNT;
}
#pragma mark - 为item赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        LKCycleViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LK_Cell" forIndexPath:indexPath];
    // 记录当前item和section
    _currentSection = indexPath.section;
    _itemIndex = indexPath.item;
    // 加一个数据保护
    if (self.dataSourceArr.count != 0) {
        SlideModel *mdoel = self.dataSourceArr[indexPath.item];
        [cell.cycleImageView sd_setImageWithURL:[NSURL URLWithString:mdoel.host_pic]];
        
        
//        cell.cycleImageView.image = self.dataSourceArr[indexPath.item];

    }
    // 添加循环保护:(防止到最后一个分区出现崩溃)
    if (indexPath.section == SECTION_COUNT - 1) {
        [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:SECTION_COUNT / 2] atScrollPosition:(UICollectionViewScrollPositionLeft) animated:NO];
    }
    return cell;
}
#pragma mark - 手指开始滑动时定时器失效
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self removeTimer];
}
#pragma mark - 手指离开定时器开启:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self addTimer];
}
@end
